package com.weipeep.common.utils.poi;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by cdliujian1 on 2015/7/30.
 */
public class Main {

    public static final String filename = "C:\\Users\\Administrator\\Desktop\\工作簿2.xlsx";

    public static void main(String[] args) throws IOException, InvalidFormatException {

    }

    private static void testWrite() {
        ExcelHelper<UserEntity> excelHelper = new ExcelHelper<UserEntity>();
        List<UserEntity> list = new ArrayList<UserEntity>();
        list.add(new UserEntity("王雷",22,"昌平"));
        excelHelper.writeExcel(filename,list, new ExcelWriter<UserEntity>() {
            @Override
            public void writeRow(UserEntity obj) {
                addCell("姓名", obj.getName());
                addCell("年龄", obj.getAge());
                addCell("住址", obj.getAddress());
            }
        });
    }

    private static void testRead() throws Exception {
        ExcelHelper<UserEntity> excelHelper = new ExcelHelper<UserEntity>();
        ExcelResolver resolver =  new ExcelResolver<UserEntity>() {
            @Override
            public boolean resolve(UserEntity obj) {
                String value = getCellValue("姓名");
                String ageStr = getCellValue("年龄");
                String address = getCellValue("住址");
                if (ageStr !=null && !ageStr.trim().equals("")){
                    Integer age = Integer.parseInt(ageStr);
                    obj.setAge(age);
                }
                obj.setAddress(address);
                obj.setName(value);
                return true;
            }
        };
        List<UserEntity> list = excelHelper.readExcel(filename, UserEntity.class,resolver );
        System.out.println(list.size());
    }


}
